package com.ivr.cm.handler;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

/**
 * Action工厂，用户从这里获得真正的Action
 * 和高阳vspp隔离
 * @author liugeng
 * @date 2015-12-15
 *
 */
public class HandlerFactory {
	private static Logger logger = Logger.getLogger(HandlerFactory.class);
	
	public BaseHandler defaultHandler;
	
	/**
	 * 根据serviceid和operateid来获取相应的handler
	 * 根据约定，handler的class名称为com.ivr.cm.handler.impl.Handler_125906586_00017_20001，其中00017为serviceid,operateid为20001
	 * 注：getHandler获取的handler理论上可以保持单例模式，因不确定旧的hanlder在单例下能不能正常运行，因此没有这么做
	 * @return
	 */
	public BaseHandler getHandler(String spnumber,String serviceid, String operateid){
//		if("125904030278".equals(spnumber)){
//			spnumber = "125907771";
//		}else if("125904030279".equals(spnumber)){
//			spnumber = "125907772";
//		}else if("125904030280".equals(spnumber)){
//			spnumber = "125907773";
//		}else if("125904030281".equals(spnumber)){
//			spnumber = "125907774";
//		}else if("125904030282".equals(spnumber)){
//			spnumber = "125907775";
//		}
		
		String classname = "com.ivr.cm.handler.impl.Handler_"+spnumber+"_" + serviceid + "_" + operateid;
		
		try{
			/**根据包体内容长号码获得具体的处理handle*/
			BaseHandler handler = (BaseHandler) Class.forName(classname).newInstance();
			return handler;
		}catch (Exception e) {
			logger.error(String.format("获取handler异常:%s", ExceptionUtils.getFullStackTrace(e)));
			return defaultHandler;
		}
	}
	
	public BaseHandler getDefaultHandler() {
		return defaultHandler;
	}
	public void setDefaultHandler(BaseHandler defaultHandler) {
		this.defaultHandler = defaultHandler;
	}
}